package com.usian.controller;

import com.usian.ItemFeign;
import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/backend/item")
public class ItemController {

    @Autowired
    private ItemFeign itemFeign;

    @RequestMapping("/selectItemInfo")
    public Result selectItemInfo(Long itemId) {
        TbItem tbItem = itemFeign.selectItemInfo(itemId);
        if (tbItem != null) {
            return Result.ok(tbItem);
        }
        return Result.error("查无结果");
    }


    /**
     * 分页查询商品列表
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer rows){

        PageResult pageResult = itemFeign.selectTbItemAllByPage(page,rows);
        if(pageResult.getResult()!=null && pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("数据为空");
    }

    /**
     * 添加商品
     *
     * @param item
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("insertTbItem")
    public Result insertTbItem( TbItem item, String desc, String itemParams){
        Integer num = itemFeign.insertTbItem(item, desc, itemParams);
        if(num == 3){
            return Result.ok("OK");
        }
        return Result.error("添加失败");
    }

    /**
     *
     * 修改商品回显
     *
     * @param itemId
     * @return
     */
    @RequestMapping("preUpdateItem")
    public Result preUpdateItem(@RequestParam  Long itemId){
        Map<String, Object> map = itemFeign.preUpdateItem(itemId);
        if(map.size()>0){
            return Result.ok(map);
        }
        return Result.error("回显失败");
    }


    /**
     * 修改商品
     *
     * @param item
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("updateTbItem")
    public Result updateTbItem( TbItem item, String desc, String itemParams){
        Integer num = itemFeign.updateTbItem(item, desc, itemParams);
        if(num == 3){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 删除商品
     *
     * @param itemId
     * @return
     */
    @RequestMapping("deleteItemById")
    public Result deleteItemById( @RequestParam Long itemId){
        Integer num = itemFeign.deleteItemById(itemId);
        if(num ==3){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
